package com.hong.qrpc.spring.springboot.starter.config;

import com.hong.config.constant.ClientConfig;
import com.hong.config.constant.ServerConfig;
import com.hong.qrpc.spring.springboot.starter.postprocessor.QrpcPostProcessor;
import com.hong.qrpc.utils.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 此处将会获取到所有的dubbo相关的配置，到时候需要使用的时候直接调用方法返回一个Profile对象即可
 *
 * @Author HQT
 * @Date 2022/7/30 20:30
 */
@Configuration
@ConditionalOnBean(QrpcPostProcessor.class)
public class QrpcAutoConfiguration {
    public Map getProfile() {
        Map profile = new HashMap();
        addKV(profile, ClientConfig.QRPC_CODE_MANNER, QRPC_CODE_MANNER);
        addKV(profile, ClientConfig.QRPC_LOADBALANCE_MODE, QRPC_LOADBALANCE_MODE);
        addKV(profile, ClientConfig.QRPC_CLIENT_PORT, QRPC_CLIENT_PORT);
        addKV(profile, ServerConfig.QRPC_SERVER_PORT, QRPC_SERVER_PORT);
        addKV(profile, ClientConfig.QRPC_TRANSPORT_MANNER, QRPC_TRANSPORT_MANNER);
        addKV(profile, ClientConfig.QRPC_DISCOVER_SERVICE_ADDR, QRPC_DISCOVER_SERVICE_ADDR);
        addKV(profile, ClientConfig.QRPC_REGISTER_CENTER_TYPE, QRPC_REGISTER_CENTER_TYPE);
        addKV(profile, ServerConfig.QRPC_REGISTER_ADDR, QRPC_REGISTER_ADDR);
        addKV(profile, ServerConfig.QRPC_SERVICE_EXPORT, QRPC_SERVICE_EXPORT);
        addKV(profile, ServerConfig.QRPC_REGISTER_SCANPACKAGE, QRPC_REGISTER_SCANPACKAGE);
        addKV(profile, ClientConfig.QRPC_NUMBER_OF_TIMEOUT, QRPC_NUMBER_OF_TIMEOUT);
        addKV(profile, ClientConfig.QRPC_REQUEST_RETRIES, QRPC_REQUEST_RETRIES);
        addKV(profile, "start.by.string", START_BY_SPRING);
        addKV(profile, ClientConfig.QRPC_PROTOCOL_TYPE, QRPC_PROTOCOL_TYPE);
        return profile;
    }

    private void addKV(Map profile, String key, String value) {
        if (!StringUtils.isEmpty(value)) {
            profile.put(key, value);
        }
    }

    @Value("${qrpc.server.port:null}")
    private String QRPC_SERVER_PORT;
    @Value("${qrpc.client.port:null}")
    private String QRPC_CLIENT_PORT;
    @Value("${qrpc.protocol.type:null}")
    private String QRPC_PROTOCOL_TYPE;
    @Value("${qrpc.code.manner:null}")
    private String QRPC_CODE_MANNER;
    @Value("${qrpc.loadbalance.mode:null}")
    private String QRPC_LOADBALANCE_MODE;
    @Value("${qrpc.transport.manner:null}")
    private String QRPC_TRANSPORT_MANNER;
    @Value("${qrpc.discover.service.addr:null}")
    private String QRPC_DISCOVER_SERVICE_ADDR;
    @Value("${qrpc.register.center.type:null}")
    private String QRPC_REGISTER_CENTER_TYPE;
    @Value("${qrpc.register.addr:null}")
    private String QRPC_REGISTER_ADDR;
    @Value("${qrpc.service.export:null}")
    private String QRPC_SERVICE_EXPORT;
    @Value("${qrpc.register.scanpackage:null}")
    private String QRPC_REGISTER_SCANPACKAGE;
    @Value("${qrpc.number.of.timeout:null}")
    private String QRPC_NUMBER_OF_TIMEOUT;
    @Value("${qrpc.request.retries:null}")
    private String QRPC_REQUEST_RETRIES;
    private String START_BY_SPRING = "true";
}
